﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace antuAutoUPTools.Entries
{
    /// <summary>
    /// 异常日志文件记录
    /// </summary>
    public class ExceptionLog
    {
        //日志路径
        private static string logpath = AppDomain.CurrentDomain.BaseDirectory + "Exception_logs\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
        private static object lockobj = new object();

        //字符串日志
        public static void WriteLog(string logmessage)
        {
            try
            {
                lock (lockobj)
                {
                    logpath = System.Environment.CurrentDirectory + "Logs\\Exception_logs\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";

                    #region 创建文件夹
                    //创建文件夹
                    if (!Directory.Exists(System.Environment.CurrentDirectory + "Logs\\Exception_logs"))
                    {
                        Directory.CreateDirectory(System.Environment.CurrentDirectory + "Logs\\Exception_logs");
                    }
                    #endregion

                    #region 写错误日志
                    string logstr = DateTime.Now.ToLocalTime() + "信息: " + logmessage + Environment.NewLine;
                    File.AppendAllText(logpath, logstr);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }

        //列表日志
        public static void WriteListLog(List<string> list)
        {
            try
            {
                foreach (string logmessage in list)
                {
                    WriteLog(logmessage + ";");
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }

        /// <summary>
        /// 写异常日志
        /// </summary>
        /// <param name="e"></param>
        public static void WriteExceptionLog(Exception e)
        {
            try
            {
                WriteLog(e.Message);

                WriteLog(e.StackTrace);
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }
    }
}
